package org.canaan.entiy;

import java.io.Serializable;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;


/**
 * 用户操作后（如：修改用户，删除用户）后台统一返回的消息模型
 *
 * @author PraiseLord
 * @date 2016年3月9日
 */

public class Message implements Serializable {
    private static final long serialVersionUID = 8645327167090301453L;

    private String title;                //消息标题
    private final MessageType type;        //消息类型
    private Object content;                //消息内容
    private Integer code;                //返回代码
    private Object data;                //返回结果

    private static final String title_msg = null;
    private static final String info_msg = "";
    private static final String success_msg = "操作成功！";
    private static final String warn_msg = "操作警告！";
    private static final String error_msg = "操作失败！";
    private static final String exception_msg = "系统异常！";
    public static final Message INFO;
    public static final Message SUCCESS;
    public static final Message WARN;
    public static final Message ERROR;
    public static final Message EXCEPTION;

    private Message(String title, MessageType type, Object content, int code) {
        super();
        this.title = title;
        this.type = type;
        this.content = content;
        this.code = code;
    }

    static {
        INFO = new Message(title_msg, MessageType.info, info_msg,0);
        SUCCESS = new Message(title_msg, MessageType.success, success_msg,0);
        ERROR = new Message(title_msg, MessageType.error, error_msg,-1);
        WARN = new Message(title_msg, MessageType.warn, warn_msg,0);
        EXCEPTION = new Message(title_msg, MessageType.exception, exception_msg,-1);
    }

    public static Message info(Object content) {
        return INSTANCE(MessageType.info, content,0);
    }
    public static Message info(Object content,int code) {
        return INSTANCE(MessageType.info, content,code);
    }

    public static Message success(Object content) {
        return INSTANCE( MessageType.success, content,0);
    }

    public static Message success(Object content ,int code) {
        return INSTANCE( MessageType.success, content,code);
    }

    public static Message warn(Object content) {
        return INSTANCE( MessageType.warn, content,0);
    }

    public static Message warn(Object content,int code) {
        return INSTANCE( MessageType.warn, content,code);
    }

    public static Message error(Object content) {
        return INSTANCE( MessageType.error, content,-1);
    }

    public static Message error(Object content,int code) {
        return INSTANCE( MessageType.error, content,code);
    }

    public static Message exception(Object content) {
        return INSTANCE( MessageType.exception, content,-1);
    }

    public static Message exception(Object content,int code) {
        return INSTANCE(MessageType.exception, content,code);
    }

    /**
     * 根据数据操作的影响结果，返回消息相应的消息对象
     *
     * @param effectRow
     * @return
     */
    public static Message get(int effectRow) {
        if (effectRow > 0) {
            return Message.SUCCESS;
        }
        return Message.ERROR;
    }

    /**
     * 参考 Message#get(int effectRow, String title,String successMsg,String errorMsg)
     */
    public static Message get(int effectRow, String successMsg, String errorMsg) {
        if (effectRow > 0) {
            return Message.success(successMsg);
        }
        return Message.error(errorMsg);
    }

    /**
     * 根据数据库返回的影响数来决定消息类型，规则：{@code effectRows} >0 success 否则为 error
     *
     * @return
     */
    public static Message get(int effectRow, String successMsg, String errorMsg, int code) {
        if (effectRow > 0) {
            return Message.success(successMsg,code);
        }

        return Message.error(errorMsg,code);
    }

    /**
     * 创建一个消息实体
     *
     * @param type
     * @param content
     * @return
     */
    public static Message INSTANCE(MessageType type, Object content, int code) {
        return new Message(title_msg,type, content,code);
    }

    @JSONField(serialize = false)    //fastjson 注解
    public boolean isInfo() {
        return this.type == MessageType.info;
    }

    public boolean isSuccess() {
        return this.type == MessageType.success;
    }

    @JSONField(serialize = false)
    public boolean isWarn() {
        return this.type == MessageType.warn;
    }

    @JSONField(serialize = false)
    public boolean isError() {
        return this.type == MessageType.error || this.type == MessageType.exception;
    }
    @JSONField(serialize = false)
    public boolean isException() {
        return this.type == MessageType.exception;
    }

    /**
     * 如果消息类型为error,或exception那么就抛出
     *
     * @param message
     */
    public void throwIfError(String message) {
        if (isError() || isException()) {
            this.content = message;
            throw new IllegalStateException(message);
        }
    }

    /**
     * 如果消息类型为error,或exception那么就抛出
     */
    public void throwIfError() {
        if (isError() || isException()) {
            throw new IllegalStateException(content.toString());
        }
    }

    public String getTitle() {
        return title;
    }

    public MessageType getType() {
        return type;
    }

    public Object getContent() {
        return content;
    }

    public Object getData() {
        return data;
    }

    /**
     * 设置消息的附加数据
     *
     * @param data
     */
    public void setData(Object data) {
        this.data = data;
    }

    /**
     * 设置消息的内容
     *
     * @param content
     */
    public void setContent(Object content) {
        this.content = content;
    }

    /**
     * 设置消息的标题
     *
     * @param title
     */
    public void setTitle(String title) {
        this.title = title;
    }

    public int getCode() {
        return code;
    }
    public void setCode(int code) {
        this.code = code;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    /**
     * @author PraiseLord
     * @date 2016年3月9日
     */
    public enum MessageType {
        info,            //系统提示
        success,        //操作成功
        warn,            //警告
        error,            //操作失败
        exception;        //系统异常
    }
}
